package com.luobu.javaee.controller;

import com.luobu.javaee.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public String login(@RequestBody Map<String, String> requestBody) {
        System.out.println(requestBody);
        String username = requestBody.get("username");
        String password = requestBody.get("password");
        System.out.println("登录："+username+"   "+password);
        String token = userService.login(username, password);
        System.out.println(token);
        return token;
    }

    @PostMapping("/register")
    public String register(@RequestBody Map<String, String> requestBody) {
        String username = requestBody.get("username");
        String password = requestBody.get("password");
        String email = requestBody.get("email");
        String code = requestBody.get("code");
        return userService.register(username, password, email, code);
    }

    @PostMapping("/email")
    public String email(@RequestBody Map<String, String> requestBody) {
        String email = requestBody.get("email");
        return userService.email(email);
    }

    @PostMapping("/logout")
    public String logout(String username) {
        return userService.logout(username);
    }

    @PostMapping("/test")
    public String test() {
        return "test";
    }

}
